import {
  reqLeaveWorld,
  reqAddLeaveWorld,
  reqRemoveLeaveWorld,
  reqAddReply,
  reqRemoveReply,
  reqBlogs,
  reqMusic,
  reqMusicLike,
  reqAddBlog,
  reqBlogDetails,
  reqRemoveBlog,
  reqCases,
  reqRemoveCase,
  reqUser,
  reqIntroduce,
  reqSingleIntroduce,
  reqAddCases,
  reqFabulous,
  reqStep,
  reqSearchBlogs
} from "../api";
import {
  RECEIVE_LEAVEWORLD,
  RECEIVE_BLOGS,
  RECEIVE_MUSIC,
  RECEIVE_BLOG_DETAILS,
  RECEIVE_CASES,
  RECEIVE_USER,
  RECEIVE_INTRODUCE,
  RECEIVE_SINGLE_INTRODUCE,
  RECEIVE_SEARCH_BLOGS
} from "./mutation-types";

export default {
  async getLeaveWorld({commit}) {
    const result = await reqLeaveWorld()
    if (result.status === 200) {
      const leaveWorld = result.data.reverse()
      commit(RECEIVE_LEAVEWORLD, {leaveWorld})
    }
  },
  async addLeaveWorld({commit}, LeaveWorld) {
    const result = await reqAddLeaveWorld(LeaveWorld)
    if (result.status === 200) {
      console.log('添加成功')
    }
  },
  async RemoveLeaveWorld({commit}, id) {
    const result = await reqRemoveLeaveWorld(id)
    if (result.status === 200) {
      console.log('删除成功')
    }
  },
  async addReply({commit}, reply) {
    const result = await reqAddReply(reply)
    if (result.status === 200) {
      console.log('添加成功')
    }
  },
  async removeReply({commit}, reply) {
    const result = await reqRemoveReply(reply)
    if (result.status === 200) {
      console.log('删除成功')
    }
  },
  async getBlogs({commit}) {
    const result = await reqBlogs()
    if (result.status === 200) {
      const blogs = result.data.reverse()
      commit(RECEIVE_BLOGS, {blogs})
    }
  },
  async getMusic({commit}) {
    const result = await reqMusic()
    if (result.status === 200) {
      const music = result.data
      commit(RECEIVE_MUSIC, {music})
    }
  },
  async getMusicLike({commit}, likes) {
    const result = await reqMusicLike(likes)
    if (result.status === 200) {
      console.log('修改成功')
    }
  },
  async addBlog({commit}, blog) {
    const result = await reqAddBlog(blog)
    if (result.status === 200) {
      console.log('添加成功')
    }
  },
  async blogDetails({commit}, blogId) {
    const result = await reqBlogDetails(blogId)
    if (result.status === 200) {
      const blogDetails = result.data
      commit(RECEIVE_BLOG_DETAILS, blogDetails)
    }
  },
  async removeBlog({commit}, blogId) {
    const result = await reqRemoveBlog(blogId)
    if (result.status === 200) {
      console.log('删除成功')
    }
  },
  async getCases({commit}) {
    const result = await reqCases()
    if (result.status === 200) {
      const cases = result.data.reverse()
      commit(RECEIVE_CASES, cases)
    }
  },
  async removeCase({commit}, caseId) {
    const result = await reqRemoveCase(caseId)
    if (result.status === 200) {
      console.log('删除成功')
    }
  },
  async getUser({commit}, user) {
    const result = await reqUser(user)
    if (result.status === 200) {
      const User = result.data
      commit(RECEIVE_USER, User)
      location.reload();
    } else {
      alert('用户名密码错误')
    }
  },
  async getIntroduce({commit}) {
    const result = await reqIntroduce()
    if (result.status === 200) {
      const introduce = result.data
      commit(RECEIVE_INTRODUCE, introduce)
    }
  },
  async getSingleIntroduce({commit}, id) {
    const result = await reqSingleIntroduce(id)
    if (result.status === 200) {
      const singleIntroduce = result.data
      if (singleIntroduce.hobby) {
        singleIntroduce.hobby = singleIntroduce.hobby.split(',')
      }
      commit(RECEIVE_SINGLE_INTRODUCE, singleIntroduce)
    }
  },
  async addCases({commit}, cases) {
    const result = await reqAddCases(cases)
    if (result.status === 200) {
      console.log('添加成功')
    }
  },
  async fabulous({commit}, blog) {
    const result = await reqFabulous(blog)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  async step({commit}, blog) {
    const result = await reqStep(blog)
    if (result.status === 200) {
      console.log('ok')
    }
  },
  async searchBlogs({commit}, keyword) {
    const result = await reqSearchBlogs(keyword)
    if (result.status === 200) {
      console.log(result.data)
      const blogs = result.data
      commit(RECEIVE_SEARCH_BLOGS, blogs)
    }
  },
}